package com.all.learning.guava.other;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.MapDifference;
import com.google.common.collect.MapDifference.ValueDifference;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
import org.junit.Test;
public class GGcollect {

	

	private static void test7() {
		//ConcurrentMap<String, String> cache = new MapMaker()//
		
	}

	/**
	 * 集合的创建
	 */
	private static void test6() {
		List<String> list = Lists.newArrayList("xxx", "jfjfj");
//		Lists.partition(list, 1);
		Map<String, String> map = Maps.newHashMap();
		//不可变
		ImmutableList<Integer> l = ImmutableList.of(1, 3, 4, 5);
		ImmutableMap<String, Integer> m = ImmutableMap.of("1", 1, "2", 2, "3", 3);
		//不可变   后家元素
		ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
		builder.put("1", 1);
		

	}

	/**
	 * Map<K, List<V>>
	 */
//	@Test
	private static void test5() {
		Multimap<Integer, String> siblings2 = ArrayListMultimap.create();
		Multimap<Integer, String> siblings = HashMultimap.create();
		siblings.put(0, "Kenneth");
		siblings.put(1, "Joe");
		siblings.put(2, "John");
		siblings.put(3, "Jerry");
		siblings.put(3, "Jay");
		siblings.put(3, "Jay");
		System.out.println(siblings);
		System.out.println(siblings.size());
		
		System.out.println(siblings.keys());
		System.out.println(siblings.keySet());
//		siblings.
		
//		for (int i = 0; i < siblings.size(); i++) {
//			int freq = siblings.get(i).size();
//			System.out.printf("%s siblings frequency %s\n", i, freq);
//		}
	}

	/**
	 * map的交集, 并集, 差集
	 */
	private static void test4() {
		ImmutableMap<String, String> map1 = ImmutableMap.of("key1", "value3", "key2", "value2");
		ImmutableMap<String, String> map2 = ImmutableMap.of("key2", "value2", "key1", "value1");

		MapDifference<String, String> diff = Maps.difference(map1, map2);
		System.out.println(diff.areEqual());

		// key相同，value不同
		Map<String, ValueDifference<String>> ent = diff.entriesDiffering();
		System.out.println(ent);

		Map<String, String> left = diff.entriesOnlyOnLeft();
		System.out.println("不同部分" + left);

		Map<String, String> comm = diff.entriesInCommon();
		System.out.println("相同" + comm);
	}

	/**
	 * Set交集, 差集, 交集
	 */
	@Test
	public void testSets() {
		HashSet<Integer> newSet = Sets.newHashSet(1, 2, 3, 7, 8);
		HashSet<Integer> oldSet = Sets.newHashSet(4, 5, 3, 1,2,6);
//		SetView<Integer> union = Sets.union(newSet, oldSet);
//		System.out.println(union);
//
		SetView<Integer> diff = Sets.difference(newSet, oldSet);//差集
		System.out.println(diff);

//		SetView<Integer> inter = Sets.intersection(newSet, oldSet);//交集
//		System.out.println(inter);

	}

	/**
	 * 不可变集合
	 */
	public static void test2() {
		ImmutableList<String> list = ImmutableList.of("a", "b", "c", "d");
		ImmutableMap<String, String> map = ImmutableMap.of("key1", "value", "key2", "value2");
		System.out.println(list);
		System.out.println(map);
	}

	@Test
	public  void test1() {
		
		Multimap<String, String> map = ArrayListMultimap.create();
		// Adding some key/value
		map.put("Fruits", "Bannana");
		map.put("Fruits", "Apple");
		map.put("Fruits", "Pear");
		map.put("Fruits", "Pear");
		map.put("Vegetables", "Carrot");
		map.put("cc", "Carrot");
		map.put("yy", "Carrot");
		
//		Set<String> sets = map.keySet();
		testMap(map);
		System.out.println("===="+map.keySet());
		
		// Getting the size
//		Integer size = map.size();
//		System.out.println(size);// 5

		// Getting values
//		Collection<String> list = map.get("Fruits");
//		System.out.println(list);// [Bannana, Apple, Pear, Pear]
//		System.out.println(ImmutableSet.copyOf(list));// [Bannana, Apple, Pear]
//
//		for (String value : map.values()) {
//			System.out.println(value);// 5size
//		}

//		map.remove("Fruits", "Apple");
////		System.out.println(map.get("Fruits"));
//
//		map.removeAll("Fruits");
////		System.out.println(map.get("Fruits"));
////		map.clear();
//		System.out.println(map.keySet());// 所有要发送的盒子编号);
	}
	
	
	public void testMap(Multimap<String, String> map){
		Set<String> sets = Sets.newHashSet(map.keySet());
		System.out.println("开始"+sets);
		for (String string : sets) {
			if("yy".equals(string)){
				map.removeAll(string);
				map.removeAll("cc");
			}
//			System.out.println("---"+string);
		}
		System.out.println("结束"+sets);
		
	}
	
	public static void main(String[] args) {
//		HashSet<Integer> newSet = Sets.newHashSet();
//		HashSet<Integer> oldSet = Sets.newHashSet(9,1);
//		SetView<Integer> inter = Sets.intersection(newSet, oldSet);//交集
//		System.out.println(inter);
		System.out.println("xxx");
		test5();
	}

}
